package lib.self.ex.decor;

import android.content.Context;
import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import lib.self.R;
import lib.self.ex.ParamsEx.TitleBarParams;
import lib.self.util.autoFit.FitDpUtil;
import lib.self.util.view.LayoutUtil;
import lib.self.util.view.ViewUtil;

/**
 * description:
 * author: Steven Xiao
 * date 2018/7/2
 */
public class CustomToolBar extends Toolbar {
    private static final String TAG = TitleBarEx.class.getSimpleName();

    private static final int MATCH_PARENT = ViewGroup.LayoutParams.MATCH_PARENT;
    private static final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;

    private LinearLayout mLayoutLeft;
    private LinearLayout mLayoutRight;
    private LinearLayout mLayoutMid;

    public CustomToolBar(Context context) {
        super(context);
        init();
    }

    private void init() {
        setId(R.id.title_bar);
        /**
         * 背景色 ,先找图片，再找颜色
         */
        int bgDrawableId = TitleBarParams.getBgDrawableId();
        if (bgDrawableId != 0) {
            setBackgroundResource(bgDrawableId);
        } else {
            int bgColor = TitleBarParams.getBgColor();
            if (bgColor != 0) {
                setBackgroundColor(bgColor);
            }
        }

        //添加左边布局
        mLayoutLeft = new LinearLayout(getContext());
        mLayoutLeft.setOrientation(LinearLayout.HORIZONTAL);
        Toolbar.LayoutParams leftParams = new Toolbar.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftParams.gravity = Gravity.LEFT;
        addView(mLayoutLeft, leftParams);

        //添加中间布局
        mLayoutMid = new LinearLayout(getContext());
        mLayoutMid.setOrientation(LinearLayout.HORIZONTAL);
        Toolbar.LayoutParams midParams = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
        midParams.gravity = Gravity.CENTER_HORIZONTAL;
        addView(mLayoutMid, midParams);

        //添加右边布局
        mLayoutRight = new LinearLayout(getContext());
        mLayoutRight.setOrientation(LinearLayout.HORIZONTAL);
        Toolbar.LayoutParams rightParams = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
        rightParams.gravity = Gravity.RIGHT;
        addView(mLayoutRight, rightParams);
    }

    /**
     * 加入自定义布局 左对齐
     *
     * @param view
     * @param listener
     */
    public void addViewLeft(View view, OnClickListener listener) {
        LinearLayout.LayoutParams params = LayoutUtil.getLinearParams(WRAP_CONTENT, WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        if (listener != null) {
            view.setOnClickListener(listener);
        }
        mLayoutLeft.addView(view, params);
        show();
    }

    /**
     * 加入自定义布局 右对齐
     *
     * @param view
     * @param listener
     */
    public void addViewRight(View view, OnClickListener listener) {
        LinearLayout.LayoutParams params = LayoutUtil.getLinearParams(WRAP_CONTENT, WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        if (listener != null) {
            view.setOnClickListener(listener);
        }
        mLayoutRight.addView(view, params);
        show();
    }

    /**
     * 加入自定义布局 居中
     *
     * @param view
     * @param listener
     */
    public void addViewMid(View view, OnClickListener listener) {
        addViewMid(view, LayoutUtil.getLinearParams(WRAP_CONTENT, WRAP_CONTENT), listener);
    }

    /**
     * 加入自定义布局 居中
     *
     * @param view
     * @param params
     * @param listener
     */
    public void addViewMid(View view, LinearLayout.LayoutParams params, OnClickListener listener) {
        params.gravity = Gravity.CENTER;
        if (listener != null) {
            view.setOnClickListener(listener);
        }
        mLayoutMid.addView(view, params);
        show();
    }

    private TextView getTvWithParams(int sizeDp, @ColorInt int color, int paddingHoriDp, OnClickListener listener) {
        TextView textView = new TextView(getContext());
        textView.setGravity(Gravity.CENTER);

        //设置文字大小
        if (sizeDp != 0) {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, dpToPx(sizeDp));
        }

        //设置文字颜色
        if (color != 0) {
            textView.setTextColor(color);
        }

        if (paddingHoriDp != 0) {
            int px = dpToPx(paddingHoriDp);
            textView.setPadding(px, 0, px, 0);
        }
        if (listener != null) {
            textView.setOnClickListener(listener);
        }
        return textView;
    }

    private LinearLayout.LayoutParams getTextLinearParams() {
        LinearLayout.LayoutParams params = LayoutUtil.getLinearParams(WRAP_CONTENT, WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        return params;
    }

    /**
     * 设置TextView Left相关
     */
    public void addTextViewLeft(CharSequence text, OnClickListener listener) {
        TextView textView = getTvWithParams(TitleBarParams.getTextSizeLeftDp(), TitleBarParams.getTextColor(),
                TitleBarParams.getTextMarginHorizontalDp(), listener);
        setTvText(text, textView);
        mLayoutLeft.addView(textView, getTextLinearParams());
    }

    public void addTextViewLeft(@StringRes int resId, OnClickListener listener) {
        addTextViewLeft(getString(resId), listener);
    }

    public void addTextViewLeft(CharSequence text, @ColorInt int color, OnClickListener listener) {
        TextView textView = getTvWithParams(TitleBarParams.getTextSizeLeftDp(), color,
                TitleBarParams.getTextMarginHorizontalDp(), listener);
        setTvText(text, textView);
        mLayoutLeft.addView(textView, getTextLinearParams());
    }

    /**
     * 添加中间的文字
     */
    public TextView addTextViewMid(CharSequence text, @ColorInt int color, int maxLength) {
        if (TextUtils.isEmpty(text)) {
            return null;
        }

        if (text.length() > 8) {
            text = text.subSequence(0, 8) + "...";
        }

        TextView textView = getTvWithParams(TitleBarParams.getTextSizeMidDp(), color, 0, null);
        if (maxLength != 0) {
            textView.setMaxWidth((int) (maxLength * TitleBarParams.getTextSizeMidDp() * FitDpUtil.getDensity(getContext())));
            textView.setSingleLine();
            textView.setEllipsize(TruncateAt.END);
        }
        setTvText(text, textView);
        mLayoutMid.addView(textView, getTextLinearParams());
        return textView;
    }

    public TextView addTextViewMid(CharSequence text) {
        return addTextViewMid(text, TitleBarParams.getTextColor());
    }

    public TextView addTextViewMid(CharSequence text, @ColorInt int color) {
        return addTextViewMid(text, color, 0);
    }

    public TextView addTextViewMid(@StringRes int resId, @ColorInt int color) {
        if (resId != 0) {
            return null;
        }
        return addTextViewMid(getString(resId), color);
    }

    /**
     * 添加右边的文字
     */
    public TextView addTextViewRight(CharSequence text, OnClickListener listener) {
        TextView textView = getTvWithParams(TitleBarParams.getTextSizeRightDp(), TitleBarParams.getTextColor(),
                TitleBarParams.getTextMarginHorizontalDp(), listener);
        setTvText(text, textView);
        mLayoutRight.addView(textView, getTextLinearParams());
        return textView;
    }

    public TextView addTextViewRight(CharSequence text, @ColorInt int colorId, OnClickListener listener) {
        TextView textView = getTvWithParams(TitleBarParams.getTextSizeRightDp(), colorId,
                TitleBarParams.getTextMarginHorizontalDp(), listener);
        setTvText(text, textView);
        mLayoutRight.addView(textView, getTextLinearParams());
        return textView;
    }

    public TextView addTextViewRight(@StringRes int resId, OnClickListener listener) {
        return addTextViewRight(getString(resId), listener);
    }

    private void setTvText(CharSequence text, TextView textView) {
        if (!TextUtils.isEmpty(text)) {
            textView.setText(text);
        }
        show();
    }

    public void gone() {
        ViewUtil.goneView(this);
    }

    public void show() {
        ViewUtil.showView(this);
    }

    private String getString(@StringRes int resId) {
        return getContext().getString(resId);
    }

    public LinearLayout getLayoutMid() {
        return mLayoutMid;
    }

    public LinearLayout getLayoutLeft() {
        return mLayoutLeft;
    }

    public LinearLayout getLayoutRight() {
        return mLayoutRight;
    }

    private int dpToPx(float dp) {
        return FitDpUtil.dpToPx(dp, getContext());
    }

    public void setBackgroundAlpha(@IntRange(from = 0, to = 255) int alpha) {
        getBackground().setAlpha(alpha);
    }
}
